#!/bin/bash
FILE=/proc/net/dev
FILEp=/tmp/ProcNetDev
# Check existance of previous state file
if [ ! -f $STATE_FILE ] ; then 
    echo "Warning: There is no prev state file!"
    cat $FILE > $FILEp
    exit 1
fi
DATE=$(date +%s)
DATEp=$(stat -c %Y $FILEp)
ELAP=$((DATE-DATEp))
# Parameters how much traffic was routed over devices
TRAFF="INT RBYTES RPACKETS RERRS RDROP RFIFO RFRAME RCOMPRESSED RMULTICAST TBYTES TPACKETS TERRS TDROP TFIFO TCOLLS TCARRIER TCOMPRESSED "

while read $TRAFF ; do # Read traffic parametres
    if [[ "$INT" =~ eth[0-9]+: ]] ; then # Exlude head and loopback interface
        INT=${INT^^} INT=${INT%:} # Convert interface name to UPPERcase
        for i in $TRAFF ; do
            eval ${INT}_$i=${!i}
        done
        INTERFACES="${INTERFACES}$INT " # Interfaces list
    fi
done < $FILE

while read $TRAFF ; do # Convert and read traffic parametres from previous state
    if [[ "$INT" =~ eth[0-9]+: ]] ; then # Exlude head and loopback interface
        INT=${INT^^} INT=${INT%:} # Convert interface name to UPPERcase
        for i in $TRAFF ; do
            eval ${INT}_${i}p=${!i}
        done
    fi
done < $FILEp

cat $FILE > $FILEp

# calculate difference NOW - PREVIOUS, and SUM
#for i in $TRAFF ; do
#    eval $i=$(($i-${i}p))
#done

for i in $INTERFACES ; do
    for j in $TRAFF ; do
    echo ${i}_${j}=$(((${i}_${j}-${i}_${j}p)/ELAP))
    done
    echo
done
